package com.zhaolang.qogir.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.zhaolang.qogir.R;
import com.zhaolang.qogir.cache.SharePreCacheHelper;
import com.zhaolang.qogir.domain.BaseCall;
import com.zhaolang.qogir.domain.Login;
import com.zhaolang.qogir.domain.Login.LoginReq;
import com.zhaolang.qogir.domain.Login.LoginResp;
import com.zhaolang.qogir.domain.ReqFactory;
import com.zhaolang.qogir.location.LocationManager;
import com.zhaolang.qogir.util.Slog;
import com.zhaolang.qogir.wifi.AllQogirManager;

public class WelcomePre extends BaseActivity {

    private RelativeLayout mCover;
    private boolean isOnPause = false;

    private static final long MIN_WAIT_TIME = 800;
    private long mStartTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_pre_layout);
        mCover = (RelativeLayout) findViewById(R.id.welcome_cover);
//		Intent intent = new Intent(this, WelcomeLogin.class);
//		intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
//		startActivity(intent);

//		finish();
//		checkStatus();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        isOnPause = false;
        LocationManager.getInstance(this).start();
        AllQogirManager.getInstance(this).startSkinBle(true);
        checkStatus();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        isOnPause = true;
        mHandler.removeMessages(MSG_TABS);
    }

    private void checkStatus() {
        String mCurrID = SharePreCacheHelper.getUserName(this);
        mCurrID = "15651723391";
        SharePreCacheHelper.setUserID(this, mCurrID);

        if (TextUtils.isEmpty(mCurrID)) {
            mCover.setVisibility(View.GONE);
            Intent intent = new Intent(this, WelcomeLogin.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            startActivity(intent);
            finish();
        } else {
            mStartTime = System.currentTimeMillis();
            mCover.setVisibility(View.VISIBLE);
            startLogin();
            mHandler.removeMessages(MSG_TABS);
            mHandler.sendEmptyMessageDelayed(MSG_TABS, 5000); //时间需加长，3秒在移动网络下有时也失败
        }
    }

    private static final int MSG_TABS = 1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            if (!isOnPause) {
                Intent intent = new Intent(WelcomePre.this, ActivityTabs.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                startActivity(intent);
                finish();
            }
        }
    };

    private void startLogin() {
        Login l = ReqFactory.buildInterface(getApplicationContext(),
                Login.class);
        String mUserID = SharePreCacheHelper.getUserName(this);
        LoginReq bean = new LoginReq();
        bean.setUsername(mUserID);
        BaseCall<LoginResp> node = new BaseCall<Login.LoginResp>() {

            @Override
            public void call(LoginResp resp) {
                // TODO Auto-generated method stub
                if (resp.isRequestSuccess()) {

                } else {
                    Slog.e("Login Failed !!! : " + resp.getMessage());
                }
                mHandler.removeMessages(MSG_TABS);
                long time = MIN_WAIT_TIME - (System.currentTimeMillis() - mStartTime);
                time = time < 0 ? 1 : time;
                mHandler.sendEmptyMessageDelayed(MSG_TABS, time);
//				mHandler.sendEmptyMessage(MSG_TABS);
            }

        };
        addCallback(node);
        l.loginAsync(bean, node);
    }

}
